#include "stdafx.h"
#include "Entity.h"
#include "Part.h"

namespace stl
{
	CPart::CPart()
	{

	}

	CPart::~CPart()
	{
		RemoveAllEntity();
	}

	void CPart::Draw(COpenGLDC* pDC)
	{
		for (int i = 0; i < m_entList.GetSize(); ++i)
		{
			m_entList[i]->Draw(pDC);
		}
	}

	void CPart::Serialize(CArchive & ar)
	{
		CEntity::Serialize(ar);

		m_entList.Serialize(ar);
		
		if (ar.IsLoading())
		{
			m_bModified = TRUE;
		}
	}

	void CPart::AddEntity(CEntity* pEnt)
	{
		m_entList.Add(pEnt);
		m_bModified = TRUE;
	}

	BOOL CPart::RemoveEntity(CEntity* pEnt)
	{
		for (int i = 0; i < m_entList.GetSize(); ++i)
		{
			if (pEnt == m_entList[i])
			{
				m_entList.RemoveAt(i);
				m_bModified = TRUE;
				return TRUE;
			}
		}
		return FALSE;
	}

	void CPart::RemoveAllEntity()
	{
		for (int i = 0; i < m_entList.GetSize(); ++i)
		{
			GL_SAFE_DELETE(m_entList[i]);
		}

		m_entList.RemoveAll();
		m_bModified = TRUE;
	}

	BOOL CPart::IsEmpty()
	{
		return m_entList.GetSize() == 0;
	}

	UINT CPart::GetEntitySize()
	{
		return m_entList.GetSize();
	}

	stl::CEntity* CPart::GetEntity(UINT uIndex)
	{
		if (uIndex < (UINT)m_entList.GetSize())
			return m_entList[uIndex];

		return NULL;
	}

	void CPart::UpdataBox()
	{
		if (m_pBox)
		{
			GL_SAFE_DELETE(m_pBox);
		}

		CBox3D box;
		for (int i = 0; i < m_entList.GetSize(); ++i)
		{
			if (m_entList[i]->GetBox(box))
			{
				if (m_pBox)
				{
					*m_pBox += box;
				}
				else
				{
					m_pBox = new CBox3D();
					*m_pBox = box;
				}
			}
		}

		m_bModified = TRUE;
	}

}